/*
 * MapBuffer.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui.map;

import org.fpdev.util.gui.MapCoordinates;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import org.fpdev.core.basenet.BaseNetwork;
import org.fpdev.apps.rtemaster.RouteMaster;

/**
 *
 * @author demory
 */
public class MapBuffer {

  private RouteMaster av_;
  private MapPanel panel_;
  private BaseNetwork baseNet_;
  private volatile Image[] tiles_;
  public final static int NORTH = 0;
  public final static int NORTHEAST = 1;
  public final static int EAST = 2;
  public final static int SOUTHEAST = 3;
  public final static int SOUTH = 4;
  public final static int SOUTHWEST = 5;
  public final static int WEST = 6;
  public final static int NORTHWEST = 7;

  public MapBuffer(RouteMaster av, MapPanel panel) {
    av_ = av;
    panel_ = panel;
    baseNet_ = av.getEngine().getBaseNet();
    tiles_ = new Image[8];

  }

  public Image getTile(int dir) {
    return tiles_[dir];
  }

  public void clear() {
    for (int i = 0; i < 8; i++) {
      tiles_[i] = null;
    }
  }

  public synchronized void initTile(int dir) {

    BufferedImage img = new BufferedImage(panel_.getWidth(), panel_.getHeight(), BufferedImage.TYPE_INT_RGB);
    //img.setAccelerationPriority(1);
    Graphics2D g2d = img.createGraphics();
    RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHints(renderHints);

    g2d.setColor(panel_.getBGColor());
    g2d.fillRect(0, 0, panel_.getWidth(), panel_.getHeight());

    double x1, y1, x2, y2;

    MapCoordinates coords = panel_.getCanvas().getCC();

    if (dir == NORTHWEST || dir == WEST || dir == SOUTHWEST) {
      x1 = coords.getX1() - coords.getXRange();
      x2 = coords.getX1();
    } else if (dir == NORTHEAST || dir == EAST || dir == SOUTHEAST) {
      x1 = coords.getX2();
      x2 = coords.getX2() + coords.getXRange();
    } else {
      x1 = coords.getX1();
      x2 = coords.getX2();
    }

    if (dir == SOUTHWEST || dir == SOUTH || dir == SOUTHEAST) {
      y1 = coords.getY1() - coords.getYRange();
      y2 = coords.getY1();
    } else if (dir == NORTHWEST || dir == NORTH || dir == NORTHEAST) {
      y1 = coords.getY2();
      y2 = coords.getY2() + coords.getYRange();
    } else {
      y1 = coords.getY1();
      y2 = coords.getY2();
    }

    MapCanvas canvas = new MapCanvas(g2d, new MapCoordinates(x1, y1, x2, y2, panel_.getWidth(), panel_.getHeight()), baseNet_);
    //System.out.println(x1+","+y1+" to "+x2+","+y2);
    //coords.updateRange(x1, y1, x2, y2);

    //panel_.updateBase(canvas);

    panel_.getLayers().clearVisibleLinks(av_.getEngine().getScenarios());
    baseNet_.updateVisibleLinks(x1, y1, x2, y2, panel_.getLayers(), canvas.getMajorCode());
    //if(layers_.getDrawnLinks() != null) System.out.println("about to draw "+panel_.getLayers().getDrawnLinks().size()+" links");
    panel_.getLayers().draw(canvas, false);

    /* test "X" part 2
    g2d.setStroke(new BasicStroke(8));
    g2d.setColor(c);
    g2d.drawLine(0, panel_.getHeight(), panel_.getWidth(), 0);*/

    tiles_[dir] = img;
  /*int sum = 0;
  System.out.println("set image for "+dir+" dim "+img.getWidth()+"x"+img.getHeight());
  for(int x=0; x<img.getWidth(); x++) {
  for(int y=0; y<img.getHeight(); y++) {
  sum += img.getRGB(x, y);
  }
  }
  System.out.println(" sum="+sum);*/
  }
}
